home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ddj0190.arc / DUNTEMAN.LST < prev    next >
File List  |  1989-12-19  |  4KB  |  194 lines

  1. _STRUCTURED PROGRAMMING COLUMN_
  2. by Jeff Duntemann
  3.  
  4. [LISTING ONE]
  5.  
  6. PROGRAM PolyTest;  { For QuickPascal 1.0 }
  7.  
  8.            { BY Jeff Duntemann   }
  9.            { For DDJ 1/90        }
  10.  
  11. USES Crt;
  12.  
  13. TYPE
  14.   FatherPtr = ^Father;
  15.   Father = OBJECT
  16.          Age : Integer;
  17.          PROCEDURE Init;
  18.          PROCEDURE Talk;
  19.        END;
  20.  
  21.   SonPtr = ^Son;
  22.   Son    = OBJECT(Father)
  23.          Girlfriends : Integer;
  24.          PROCEDURE Init; OVERRIDE;
  25.          PROCEDURE Talk; OVERRIDE;
  26.        END;
  27.  
  28.  
  29.  
  30. VAR
  31.   Dad     : Father;
  32.   Twerp   : Son;
  33.   Person  : Father;
  34.   Link    : FatherPtr;
  35.   KidLink : SonPtr;
  36.  
  37.  
  38. PROCEDURE Father.Init;
  39.  
  40. BEGIN
  41.   Self.Age := 42;
  42. END;
  43.  
  44.  
  45. PROCEDURE Father.Talk;
  46.  
  47. BEGIN
  48.   Writeln('This is the old man talking.');
  49. END;
  50.  
  51.  
  52.  
  53. PROCEDURE Son.Init;
  54.  
  55. BEGIN
  56.   Self.Age := 11;
  57.   Self.Girlfriends := 5;
  58. END;
  59.  
  60.  
  61. PROCEDURE Son.Talk;
  62.  
  63. BEGIN
  64.   Writeln('Cool it, daddy-o!');
  65. END;
  66.  
  67.  
  68.  
  69. BEGIN
  70.   ClrScr;
  71.  
  72.   New(Dad);  { These three statements allocate the objects on the heap }
  73.   New(Twerp);
  74.   New(Person);
  75.  
  76.   Dad.Init;        { Type Father }
  77.   Twerp.Init;      { Type Son }
  78.   Person.Init;     { Type Father }
  79.  
  80.   { The following works polymorphically in QuickPascal but not Turbo: }
  81.  
  82.   Person := Twerp;   { Assign a Son object to a Father object }
  83.   Person.Talk;         { Polymorphism allows the Son.Talk method to be }
  84.              { executed from Person, which is type Father }
  85.   Writeln('Person''s age is: ',Person.Age);  { Check the age on-screen! }
  86.   Readln;
  87.  
  88.   { Working through pointers works with either Turbo or Quick Pascal: }
  89.  
  90.   Link := @Dad;      { Link is defined as a ^Father }
  91.   Link^.Talk;        { Dad speaks here... }
  92.  
  93.   KidLink := @Twerp; { Twerp is type Son }
  94.   Link := KidLink;   { Assign a SonPtr to a FatherPtr }
  95.   Link^.Talk;        { Even tho Link is a FatherPtr, the kid talks here. }
  96.   Readln;
  97.  
  98. END.
  99.  
  100.  
  101. [LISTING TWO]
  102.  
  103. PROGRAM PolyTest;   { For Turbo Pascal 5.5 }
  104.  
  105.                     { BY Jeff Duntemann    }
  106.                     { For DDJ 1/90         }
  107.  
  108.  
  109. USES Crt;
  110.  
  111. TYPE
  112.   FatherPtr = ^Father;
  113.   Father = OBJECT
  114.              Age : Integer;
  115.              CONSTRUCTOR Init;
  116.              PROCEDURE Talk; VIRTUAL;
  117.            END;
  118.  
  119.   SonPtr = ^Son;
  120.   Son    = OBJECT(Father)
  121.              Girlfriends : Integer;
  122.              CONSTRUCTOR Init;
  123.              PROCEDURE Talk; VIRTUAL;
  124.            END;
  125.  
  126.  
  127.  
  128. VAR
  129.   Dad     : Father;
  130.   Twerp   : Son;
  131.   Person  : Father;
  132.   Link    : FatherPtr;
  133.   KidLink : SonPtr;
  134.  
  135.  
  136. CONSTRUCTOR Father.Init;
  137.  
  138. BEGIN
  139.   Age := 42;
  140. END;
  141.  
  142.  
  143. PROCEDURE Father.Talk;
  144.  
  145. BEGIN
  146.   Writeln('This is the old man talking.');
  147. END;
  148.  
  149.  
  150.  
  151. CONSTRUCTOR Son.Init;
  152.  
  153. BEGIN
  154.   Age := 11;
  155.   Girlfriends := 5;
  156. END;
  157.  
  158.  
  159. PROCEDURE Son.Talk;
  160.  
  161. BEGIN
  162.   Writeln('Cool it, daddy-o!');
  163. END;
  164.  
  165.  
  166.  
  167. BEGIN
  168.   ClrScr;
  169.  
  170.   Dad.Init;        { Type Father }
  171.   Twerp.Init;      { Type Son }
  172.   Person.Init;     { Type Father }
  173.  
  174.  
  175.   { The following does not work polymorphically in Turbo Pascal: }
  176.  
  177.   Person := Twerp;  { The assignment is kosher to the compiler...}
  178.   Person.Talk;      { ...but polymorphism does NOT work here...  }
  179.                     { ...and Dad speaks instead of the kid.      }
  180.   Writeln('Person''s age is: ',Person.Age);
  181.   Readln;
  182.  
  183.   { This, on the other hand, works polymorphically as it should: }
  184.  
  185.   Link := @Dad;       { Link is defined as a FatherPtr }
  186.   Link^.Talk;         { Dad speaks here... }
  187.  
  188.   KidLink := @Twerp;  { Twerp is type Son }
  189.   Link := KidLink;    { Assign a SonPtr to FatherPtr }
  190.   Link^.Talk;         { Even tho Link is a FatherPtr, the kid talks here. }
  191.   Readln;
  192.  
  193. END.
  194.